package com.biz.primus.ms.payment.channel.allinpay.req;

import com.biz.primus.common.utils.JsonUtils;
import com.biz.primus.ms.payment.channel.allinpay.res.AllInPayCardOpenDataRespVo;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.client.RestTemplate;

import java.util.TreeMap;
import java.util.TreeSet;

/**
 * AllInPayCardOpen
 * 开卡
 *
 * @author JKLiues
 * @date 2017年11月11日
 */
@Getter
@Setter
@Slf4j
public class AllInPayCardOpen extends AllInPayRequestBase {

    private static final String API_URL = "http://116.228.64.55:8080/aop/rest";
    /**
     * ;是	AOP分配给应用的AppKey
     */
    private String app_key;
    /**
     * ;是	API接口名称
     */
    private String method = "allinpay.ppcs.cloud.card.open";
    /**
     * ;是	时间戳，格式为yyyyMMddHHmmss，例如：20110125010101。API服务端允许客户端请求时间误差为30分钟。
     */
    private String timestamp;
    /**
     * ;是	API协议版本，可选值:1.0。
     */
    private String v = "1.0";

    /**
     * ;是	签名版本号 1 ,2,3,4,5每次更新后+1递增
     */
    private String sign_v = "1";
    /**
     * ;是	指定响应格式。默认xml,目前支持格式为xml,json
     */
    private String format = "json";
    /**
     * 合作机构订单号
     */
    private String order_id;
    /**
     * 机构号
     */
    private String brh_id;
    /**
     * 品牌号
     */
    private String brand_no;
    /**
     * 手机号
     */
    private String phone_num;
    /**
     * 密码（六位数字）
     */
    private String password;
    /**
     * 渠道号
     * 云POS为1000000000
     * 云phone为4000000000，
     * 云网为4000000001，
     * 商家服务台为4000000002
     */
    private String chan_no = "4000000002";

    public AllInPayCardOpenDataRespVo execute(String sign) {
        TreeMap<String, Object> map = new TreeMap<>();
        map.put("app_key", this.app_key);
        map.put("method", this.method);
        map.put("timestamp", this.timestamp);
        map.put("v", this.v);
        map.put("sign", sign);
        map.put("sign_v", this.sign_v);
        map.put("format", this.format);
        map.put("order_id", this.order_id);
        map.put("brh_id", this.brh_id);
        map.put("brand_no", this.brand_no);
        map.put("phone_num", this.phone_num);
        map.put("password", this.password);
        map.put("chan_no", this.chan_no);
        RestTemplate restTemplate = getRestTemplate();
        String finalUrl = buildUrl(getApiUrl(), map);
        log.info("execute get {}", finalUrl);
        String forObject = restTemplate.getForObject(finalUrl, String.class);
        log.info(forObject);
        AllInPayCardOpenDataRespVo result = JsonUtils.json2Obj(forObject, AllInPayCardOpenDataRespVo.class);
        log.info("class result:{}",result.toString());
        return result;
    }

    @Override
    protected String getApiUrl() {
        return API_URL;
    }

    @Override
    public TreeSet<String> getSignParams() {
        return Sets.newTreeSet(Lists.newArrayList(
                "app_key",
                "method",
                "timestamp",
                "v",
                "sign_v",
                "format",
                "order_id",
                "brh_id",
                "brand_no",
                "phone_num",
                "password",
                "chan_no"
        ));
    }


}